package cn.abellflower.flowerhomehandler.home.service.impl;

import cn.abellflower.flowerhomehandler.home.bo.DecryptBo;
import cn.abellflower.flowerhomehandler.home.bo.HomeBo;
import cn.abellflower.flowerhomehandler.home.enums.HomeEnum;
import cn.abellflower.flowerhomehandler.home.resp.RespView;
import cn.abellflower.flowerhomehandler.home.vo.HomeRouteColorView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @Description
 * @Author gbzdyf@126.com
 * @Date 2020/10/23 1:40 上午
 */
@Slf4j
@Service
public abstract class HomeServiceAbstract {

    public final RespView dealCommonBusiness(HomeEnum homeEnum, String paramList) {
        log.info("开始执行了-----------------");
        //1、解密
        DecryptBo decryptBo = decrypt(paramList);
        //2、验证码路由视图判断
        verifyCodeView(decryptBo);
        //3、获取订单信息
        HomeBo homeBo = getOrderInfo(decryptBo);
        //4、订单判断 返回视图模型
        RespView<HomeRouteColorView> respView = statusView(homeBo);
        if (respView.successEd()) {
            return respView;
        }
        //5、获取默认明细信息 并且匹配编码
        getDefaultDetailAndBm(homeBo);
        //6、计算封装数据
        calc(homeBo);
        //7、封装返回视图View
        HomeRouteColorView homeView = enclosureView(homeBo);
        respView.setStatus("0000");
        respView.setData(homeView);
        return  respView;
    }

    /**
     * 解密报文
     * @param paramList
     * @return
     */
    abstract DecryptBo decrypt(String paramList);

    /**
     * 验证码路由视图
     * @param decryptBo
     */
    private void verifyCodeView(DecryptBo decryptBo) {
        //验证吗 视图 redis key [count-verifyCode-解锁标识位]
        //此处重新扫码 如果解锁标识位会不一致 则重新赋值解锁标志位
        log.info("verifyCodeView视图哟");
    }

    /**
     * 获取默认明细信息
     * @param homeBo
     */
    protected void getDefaultDetailAndBm(HomeBo homeBo) {
        if (CollectionUtils.isEmpty(homeBo.getHomeDetailBoList())) {
            //TODO 获取默认明细逻辑
        }
        //TODO 匹配编码
        log.info("匹配编码公共逻辑哟");
    }

    void calc(HomeBo homeBo) {
        //TODO 计算数据
        log.info("计算核心逻辑哟");
    }

    /**
     * 获取报文
     * @param decryptBo
     * @return
     */
    abstract HomeBo getOrderInfo(DecryptBo decryptBo);


    /**
     * 视图模型 （开票中 开票失败 开票完成 部分开票失败）
     * 微信 支付宝 同一人 扫码 验证
     * @param homeBo
     */
    private RespView<HomeRouteColorView> statusView(HomeBo homeBo) {
        //TODO
        RespView rv = RespView.builder().data(new HomeRouteColorView()).build();
        log.info("公共成功视图哟");
        return rv;
    }

    /**
     * 开票
     * @param homeBo
     * @return
     */
    private HomeRouteColorView enclosureView(HomeBo homeBo) {
        HomeRouteColorView homeRouteColorView = new HomeRouteColorView();
        //TODO
        log.info("最终返回视图哟");
        return homeRouteColorView;
    }

}
